#!/usr/bin/perl
#
# A most un-DB-like application: Get historical data by rooting
# through the db .txt file
#
# 981001: Move ZZ and XX into the XW count
#
use Validate::Common;

$Validate::Common::chatty = 0; # turn off error msgs

$lines = 250000;

# Preliminary gunk: Try to tabulate persons' country per month, accounting
# for deletes (arg).

# translation of monthnames (correct ones)
%months = (
"Jan" => 1,
"Feb" => 2,
"Mar" => 3,
"Apr" => 4,
"May" => 5,
"Jun" => 6,
"Jul" => 7,
"Aug" => 8,
"Sep" => 9,
"Oct" => 10,
"Nov" => 11,
"Dec" => 12,
	   );


open(FILE, "tail -$lines db/persons.txt|") || die "Unable to open db file\n";
while (<FILE>) {
    chop;
    if (/^\/\//) {
	processentry() if $id;
	$id = $';
	$hasdata = 0;
    } else {
	$hasdata = 1;
	if (/^country:/) {
	    $country = $';
	} elsif (/^recdate:/) {
	    $date = $';
	} elsif (/^email:/) {
	    $email = $';
	}
    }
    ++ $linecount;
    #last if $linecount > 100000; # debug
}
# Skip the last numbers - in this application, we don't want them,
# they're from the current day.
# printtable($day);
# Find the 5 top countries
print "#Top countries:";
for $cix (sort {$ccsum{$b} <=> $ccsum{$a}} keys(%ccsum)) {
    print " $cix";
    ++ $topcount;
    last if $topcount >= 5;
}
print "\n";

sub processentry {
    return if (!$id);
    # Clean up country, if necessary based on email
    if ($country) {
	$country = cleancountry($country);
    }
    if (!$country && $email) {
	$country = substr(email2country($email), 0, 2);
    }
    # Add missing country and 2 versions of unknown country to
    # the "somewhere in the world" count
    if (!$country || $country eq "ZZ" || $country eq "XX") {
	$country = "XW";
    }
    if ($date) {
	# note that "month" should have been named "day" - history
	if ($date =~ /^\S+ (\S+) +(\d+).* (\d{4})/) {
	    # need to guard against backwards time and other weirdnesses...
	    if ($months{$1}) { # don't print on incorrect month
		$month = sprintf("%4d-%02d-%02d", $3, $months{$1}, $2);
		$day = $month;
		if ($month gt $prevmonth) {
		    if ($prevmonth && $month gt $prevmonth) {
			printtable($day);
		    }
		    $prevmonth = $month;
		} elsif ($month lt $prevmonth) {
		    #	print STDERR "Ignoring backwards jump from $prevmonth: $date\n";
		    ++ $backwards;
		}
	    }
	}
    }
    if ($country{$id}) { # was somewhere before
	-- $cc{$country{$id}};
	-- $totcount;
    }
    # If recdate is missing, assume it is a deleted entry
    if (!$hasdata) {
	delete $country{$id};
    } else {
	$country = "XX" if (!$country);
	++ $cc{$country};
	++ $totcount;
	$country{$id} = $country;
    }
    undef $id;
    undef $country;
    undef $date;
    undef $email;
}


sub printtable {

    my $label = shift;
    for $cix (sort(keys(%cc))) {
	print "$label:$cix:$cc{$cix}\n";
	$ccsum{$cix} += $cc{$cix};
	$cc{$cix} = 0;
    }
    print "$label:All:$totcount\n";
    $totcount = 0;
}

